package cn.wolfcode.cloud.redis;

import cn.wolfcode.cloud.shop.common.BusinessException;
import cn.wolfcode.cloud.utils.RedisCodeMsg;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Description: Redis自动装配核心类
 * @Author: 伍家钰
 * @Date: 2020/9/7 12:28
 * @Version: 1.0
 */
public class RedisService {
    @Autowired
    private JedisPool jedisPool;

    // set
    public <T> void set(KeyPrefix keyPrefix, String key, T data) {
        // java8的新特性使用完jedis后自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            // 判断是否有过期时间,则存值时设置一个过期时间
            if (keyPrefix.getExpireTime() > 0) {
                jedis.setex(realKey, keyPrefix.getExpireTime(), JSON.toJSONString(data));
            } else {
                // 没有过期时间则存值时不设置过期时间
                jedis.set(realKey, JSON.toJSONString(data));
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 如果redis出现比如连接超时时抛出一个异常
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    // get
    public <T> T get(KeyPrefix keyPrefix, String key, Class<T> clz) {
        // java8的新特性使用完jedis后自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            String realVal = jedis.get(realKey);
            if (realVal == null) {
                return null;
            }
            return JSON.parseObject(realVal, clz);
        } catch (Exception e) {
            e.printStackTrace();
            // 如果redis出现比如连接超时时抛出一个异常
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    // incr:递增1
    public Long incr(KeyPrefix keyPrefix, String key) {
        // java8的新特性使用完jedis后自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.incr(realKey);
        } catch (Exception e) {
            e.printStackTrace();
            // 如果redis出现比如连接超时时抛出一个异常
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    // decr:递减1
    public Long decr(KeyPrefix keyPrefix, String key) {
        // java8的新特性使用完jedis后自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.decr(realKey);
        } catch (Exception e) {
            e.printStackTrace();
            // 如果redis出现比如连接超时时抛出一个异常
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    // exists:是否存在
    public boolean exists(KeyPrefix keyPrefix, String key) {
        // java8的新特性使用完jedis后自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.exists(realKey);
        } catch (Exception e) {
            e.printStackTrace();
            // 如果redis出现比如连接超时时抛出一个异常
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    // expire:设置过期时间
    public Long expire(KeyPrefix keyPrefix, String key) {
        // java8的新特性使用完jedis后自动释放资源
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.expire(realKey, keyPrefix.getExpireTime());
        } catch (Exception e) {
            e.printStackTrace();
            // 如果redis出现比如连接超时时抛出一个异常
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

}
